#include "fileCommand.h"

// 给出当前文件名file_name, 其它信息由sha256查表得到
int insertFileSkip(MYSQL *mysql, filePath_t file_path, const char *file_name, const char *sha256) {
    char query[4096] = {0};
    MYSQL_RES* res;
    MYSQL_ROW row;
    int file_size;
    sprintf(query,"select file_size from FileSystem \
        where file_hash = '%s' and file_name = '%s';);",sha256,file_name);
    res = mysql_store_result(mysql);
    row = mysql_fetch_row(res);
    file_size = atoi(row[0]);
    insertCompFileElem(mysql,file_path,file_name,file_size,sha256);
    return 0;
}

// 必要的信息在 file_check 中获取
int insertFileNew(MYSQL *mysql,filePath_t file_path, const fileCheck_t *file_check) {
    printf("file_name : %s\n",file_check->file_name);
    printf("file_offset : %ld\n",file_check->file_offset);
    printf("file_size : %ld\n",file_check->file_size);
    printf("file_hash : %s\n",file_check->sha256);

    insertTempFileElem(mysql,file_path,file_check->file_name,file_check->file_offset,file_check->sha256);
    return 0;
}

int deleteFile(MYSQL* mysql,filePath_t file_content,const char* file_name) {
    char query[4096] = {0};
    sprintf(query,"update FileSystem set is_deleted  = false \
        where file_name = '%s' and user_id = %d and parent_id = %d;",
    file_name,file_content.current_user_id,file_content.current_file_id);
    int query_ret = mysql_query(mysql,query);
    if(query_ret != 0) {
        printf("文件删除操作未完成: %s\n",mysql_error(mysql));
        return -1;
    }
    return 0;
}

// 查询请求下载的文件是否存在
// 存在返回 FILE_EXIST
// 不存在返回 FILE_NOT_EXIST
int checkFileExist(MYSQL *mysql,filePath_t file_desc, const char *file_name, 
    const char *file_path) {

    MYSQL_RES* res;
    int query_ret;
    char query[4096] = {0};
    sprintf(query,"select file_id from FileSystem \
        where file_name = '%s' and file_path = '%s' and user_id = %d and is_deleted;",
        file_name,file_path,file_desc.current_user_id);
    query_ret = mysql_query(mysql,query);
    
    if(query_ret != 0) {
        return -1;
    }
    res = mysql_store_result(mysql);
    int num_size = mysql_num_rows(res);
    if(num_size == 1) {
        return FILE_EXIST;
    }

    return FILE_NOT_EXIST;
}

int isFileResume(MYSQL* mysql,const char* sha256) {
    MYSQL_RES* res;
    MYSQL_ROW row;
    int query_ret;
    char query[4096] = {0};
    sprintf(query,"select completed_file from FileSystem where file_hash = '%s' and is_deleted;",sha256);
    query_ret = mysql_query(mysql,query);

    if(query_ret != 0) {
        return -1;
    }

    res = mysql_store_result(mysql);
    int num_size = mysql_num_rows(res);

    if(num_size == 0) {
        return FILE_NOT_EXIST;
    }
    if(num_size == 1) {
        row = mysql_fetch_row(res);
        int status = atoi(row[0]);
        if(status == 1) {
            return FILE_EXIST;
        }else {
            return FILE_RESUME;
        }
    }
    return 0;
}



int updateFileInfo(MYSQL *mysql, off_t file_size, const char *sha256,int flag) {
    printf("当前分片 = %ld\n",file_size);
    char query[4096] = {0};
    char select_query[4096] = {0};
    int int_file_size = (int)(file_size);
    int query_ret;
    
    if(flag == FILE_LAST_SEND) {
        sprintf(query,"update FileSystem set file_size = %d,completed_file = true \
            where file_hash = '%s';",int_file_size,sha256);
    }
    else {
        // 查询当前文件的file_size
        sprintf(select_query,"select file_size from FileSystem \
            where file_hash = '%s' and not completed_file;",sha256);
        query_ret = mysql_query(mysql,select_query);
        if(query_ret != 0) {
            return -1;
        }
        MYSQL_RES* res;
        MYSQL_ROW row;
        int get_file_size;
        int new_file_size;
        res = mysql_store_result(mysql);
        if(mysql_num_rows(res) == 1) {
            row = mysql_fetch_row(res);
            get_file_size = atoi(row[0]);
            
            new_file_size = get_file_size + int_file_size;
        }
        printf("分片传输: 更新后的文件大小 = %d\n",new_file_size);
        while(1) {
            sleep(1);
        }
        sprintf(query,"update FileSystem set file_size = %d,completed_file = false \
            where file_hash = '%s';",new_file_size,sha256);
    }
    query_ret = mysql_query(mysql,query);
    if(query_ret != 0) {
        return -1;
    }

    return 0;
}